package com.crl.gen.service;

import com.cars.util.list.ListUtils;
import com.crl.gen.dao.GenFieldDao;
import com.crl.gen.dto.GenFieldDTO;
import com.crl.gen.model.GenField;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/22 23:01
 */
@Service
public class GenFieldService {
    @Autowired
    private GenFieldDao genFieldDao;

    /**
     * 增加字段配置
     *
     * @param genFieldDTO
     * @return
     */
    public int add(GenFieldDTO genFieldDTO) {
        GenField genField = new GenField();
        BeanUtils.copyProperties(genFieldDTO, genField);
        return genFieldDao.add(genField);
    }

    /**
     * 修改字段配置
     *
     * @param genFieldDTO
     * @return
     */
    public int update(GenFieldDTO genFieldDTO) {
        GenField genField = new GenField();
        BeanUtils.copyProperties(genFieldDTO, genField);
        return genFieldDao.update(genField);
    }

    /**
     * 删除字段配置
     *
     * @param id
     * @return
     */
    public int delete(int id) {
        return genFieldDao.delete(id);
    }


    /**
     * 字段配置列表
     *
     * @return
     */
    public List<GenFieldDTO> list(int tableId) {
        List<GenField> list = genFieldDao.list(tableId);
        List<GenFieldDTO> listResult = ListUtils.copyList(list, GenFieldDTO.class);
        return listResult;
    }


    /**
     * 获取单个字段配置
     *
     * @param id
     * @return
     */
    public GenFieldDTO get(int id) {
        GenField genField = genFieldDao.get(id);
        GenFieldDTO genFieldDTO = new GenFieldDTO();
        BeanUtils.copyProperties(genField, genFieldDTO);
        return genFieldDTO;
    }

}
